﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using TSL_Tools.SqlTools;
using ReportingEngine;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace VSLT_Reporting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int UpdateStatusBarFlagIcons(bool clearAllFlags)
        {
            int retVal = -1;
            if (clearAllFlags)
            {
                tsslTestAborted.Opacity = 0.3;
                tsslIsLocalRun.Opacity = 0.3;
                tsslHasPageResults.Opacity = 0.3;
                tsslHasTimingResults.Opacity = 0.3;
                tsslHasTransactionResults.Opacity = 0.3;
                tsslHasStepLoad.Opacity = 0.3;
                tsslHasAnalysisNotes.Opacity = 0.3;
                tsslLoadTestFailed.Opacity = 0.3;
            }
            else
            {
                int x = dgMainResults.SelectedIndex;
                DataRowView row = (DataRowView)dgMainResults.Items[x];
                int _runId;
                Int32.TryParse(row[0].ToString(), out _runId);

                retVal = _runId;

                tsslTestAborted.Opacity = re.listOfRuns[_runId].resultTypes.HasFlag(ResultTypesStoredInLoadTest.TestAborted) ? 1.0 : 0.3;
                tsslIsLocalRun.Opacity = re.listOfRuns[_runId].resultTypes.HasFlag(ResultTypesStoredInLoadTest.IsLocalRun) ? 1.0 : 0.3;
                tsslHasPageResults.Opacity = re.listOfRuns[_runId].resultTypes.HasFlag(ResultTypesStoredInLoadTest.ContainsPages) ? 1.0 : 0.3;
                tsslHasTransactionResults.Opacity = re.listOfRuns[_runId].resultTypes.HasFlag(ResultTypesStoredInLoadTest.ContainsTransactions) ? 1.0 : 0.3;
                tsslHasTimingResults.Opacity = re.listOfRuns[_runId].resultTypes.HasFlag(ResultTypesStoredInLoadTest.ContainsDetailedTimingInfo) ? 1.0 : 0.3;
                tsslHasStepLoad.Opacity = re.listOfRuns[_runId].resultTypes.HasFlag(ResultTypesStoredInLoadTest.ContainsStepLoad) ? 1.0 : 0.3;
                tsslHasAnalysisNotes.Opacity = re.listOfRuns[_runId].resultTypes.HasFlag(ResultTypesStoredInLoadTest.ContainsAnalysisNotes) ? 1.0 : 0.3;
                tsslLoadTestFailed.Opacity = re.listOfRuns[_runId].resultTypes.HasFlag(ResultTypesStoredInLoadTest.LoadTestParsingFailed) ? 1.0 : 0.3;
            }
            UpdateButtonsByRunContents();
            return retVal;
        }

        private int UpdateActiveButtons()
        {
            return UpdateActiveButtons(false); 
        }

        private int UpdateActiveButtons(bool clearAllButtons)
        {
            debugTimingInfo.Clear();

            int retVal = -1;
            RunListHighlighting highlighted;
            if (dgMainResults.SelectedItems.Count == 0 || clearAllButtons)
                highlighted = RunListHighlighting.NoRunsHighlighted;
            else if (dgMainResults.SelectedItems.Count == 1)
                highlighted = RunListHighlighting.SingleRunHighlighted;
            else
                highlighted = RunListHighlighting.MultipleRunsHighlighted;

            tvPerfmonCounters.Items.Clear();

            switch (highlighted)
            {
                case RunListHighlighting.NoRunsHighlighted:
                    #region -- No Runs Highlighted -----------------------------------------------------
                    retVal = UpdateStatusBarFlagIcons(true);
                    defaultReportName = "";

                    // Options Tab
                    btnSaveRunResults.IsEnabled = false;
                    btnPerfmonCountsForRun.IsEnabled = false;

                    // Home Tab 
                    btnShowRunResults.IsEnabled = false;
                    btnShowLoadTest.IsEnabled = false;
                    btnSaveLoadTestFile.IsEnabled = false;
                    btnGetScorecard.IsEnabled = false;
                    btnGenerateReport.IsEnabled = false;
                    btnPrintReport.IsEnabled = false;
                    btnSaveReport.IsEnabled = false;
                    btnAddPerfmonIntervalTablesToReportTab.IsEnabled = false;
                    btnAddPageIntervalTablesToReportTab.IsEnabled = false;
                    btnAddTestIntervalTablesToReportTab.IsEnabled = false;
                    btnAddTransIntervalTablesToReportTab.IsEnabled = false;
                    btnHighlightedDay.IsEnabled = false;
                    btnHighlightedRange.IsEnabled = false;
                    tbComparisonPhrase.Text = "";

                    // Table Tab 
                    btnGenPage.IsEnabled = false;
                    btnGenTest.IsEnabled = false;
                    btnGenTransaction.IsEnabled = false;
                    btnGenPerfmon.IsEnabled = false;
                    btnToggleUseIntervals.IsEnabled = false;
                    btnToggleIncludeFailed.IsEnabled = false;
                    btnToggleIncludeWarmup.IsEnabled = false;
                    btnToggleCombineNames.IsEnabled = false;

                    // Perfmon Tab
                    btnExportPerfCounters.IsEnabled = false;
                    btnImportPerfCounters.IsEnabled = false;

                    break;
                    #endregion
                case RunListHighlighting.SingleRunHighlighted:
                    #region -- One Run Highlighted -----------------------------------------------------
                    retVal = UpdateStatusBarFlagIcons(false);
                    defaultReportName = String.Format(Msg__FILENAMEFORQUICKREPORT, re.listOfRuns[retVal].LoadTestName, retVal);

                    // Options Tab
                    btnPerfmonCountsForRun.IsEnabled = true;
                    btnSaveRunResults.IsEnabled = true;

                    // Home Tab 
                    btnShowRunResults.IsEnabled = true;
                    btnShowLoadTest.IsEnabled = true;
                    btnSaveLoadTestFile.IsEnabled = true;
                    btnGetScorecard.IsEnabled = false;
                    btnGenerateReport.IsEnabled = true;
                    btnPrintReport.IsEnabled = false;
                    btnSaveReport.IsEnabled = true;
                    btnHighlightedDay.IsEnabled = true;
                    btnHighlightedRange.IsEnabled = false;
                    tbComparisonPhrase.Text = re.listOfRuns[retVal].LoadTestName;

                    // Table Tab 
                    btnGenPerfmon.IsEnabled = true;
                    btnToggleIncludeFailed.IsEnabled = true;
                    btnToggleIncludeWarmup.IsEnabled = true;
                    btnToggleCombineNames.IsEnabled = true;

                    // Perfmon Tab
                    btnExportPerfCounters.IsEnabled = true;
                    btnImportPerfCounters.IsEnabled = true;

                    // Populate the first part of the perfmon counter treeview
                    BuildInitialLoadTestTreeViewItems(retVal);
                    BuildInitialComputerTreeViewItems(retVal);

                    break;
                    #endregion
                case RunListHighlighting.MultipleRunsHighlighted:
                    #region -- Multi Runs Highlighted --------------------------------------------------
                    retVal = UpdateStatusBarFlagIcons(true);
                    defaultReportName = String.Format(Msg__FILENAMEFORSCORECARDREPORT, selectedRuns.Min(), selectedRuns.Max());

                    // Options Tab
                    btnPerfmonCountsForRun.IsEnabled = false;
                    btnSaveRunResults.IsEnabled = false;

                    // Home Tab 
                    btnShowRunResults.IsEnabled = false;
                    btnShowLoadTest.IsEnabled = false;
                    btnSaveLoadTestFile.IsEnabled = false;
                    btnGetScorecard.IsEnabled = true;
                    btnGenerateReport.IsEnabled = false;
                    btnPrintReport.IsEnabled = false;
                    btnSaveReport.IsEnabled = false;
                    btnAddPerfmonIntervalTablesToReportTab.IsEnabled = false;
                    btnAddPageIntervalTablesToReportTab.IsEnabled = false;
                    btnAddTestIntervalTablesToReportTab.IsEnabled = false;
                    btnAddTransIntervalTablesToReportTab.IsEnabled = false;
                    btnHighlightedDay.IsEnabled = false;
                    btnHighlightedRange.IsEnabled = true;
                    tbComparisonPhrase.Text = "";

                    // Table Tab 
                    btnGenPage.IsEnabled = false;
                    btnGenTest.IsEnabled = false;
                    btnGenTransaction.IsEnabled = false;
                    btnGenPerfmon.IsEnabled = false;
                    btnToggleUseIntervals.IsEnabled = false;
                    btnToggleIncludeFailed.IsEnabled = false;
                    btnToggleIncludeWarmup.IsEnabled = false;
                    btnToggleCombineNames.IsEnabled = false;

                    // Perfmon Tab
                    btnExportPerfCounters.IsEnabled = false;
                    btnImportPerfCounters.IsEnabled = false;

                    break;
                    #endregion
            }
            // currently disabled till the bugs are fixed
            //btnToggleUseIntervals.IsEnabled = false;
            return retVal;
        }


        private void UpdateButtonsByRunContents()
        {
            bool bHasTransactions = tsslHasTransactionResults.Opacity == 1.0 ? true : false;
            bool bHasPages = tsslHasPageResults.Opacity == 1.0 ? true : false;
            bool bHasTimingDetails = tsslHasTimingResults.Opacity == 1.0 ? true : false;
            bool bHasAnalysisNotes = tsslHasAnalysisNotes.Opacity == 1.0 ? true : false;
            bool bRunWasAborted = tsslTestAborted.Opacity == 1.0 ? true : false;
            bool bHasStepLoad = tsslHasStepLoad.Opacity == 1.0 ? true : false;

            btnGenTest.IsEnabled = bHasTimingDetails;
            btnGenTransaction.IsEnabled = bHasTransactions & bHasTimingDetails;
            btnGenPage.IsEnabled = bHasPages & bHasTimingDetails;
            btnToggleUseIntervals.IsEnabled = bHasStepLoad;

            btnAddTestIntervalTablesToReportTab.IsEnabled = bHasTimingDetails & bHasStepLoad;
            btnAddTransIntervalTablesToReportTab.IsEnabled = bHasTransactions & bHasTimingDetails & bHasStepLoad;
            btnAddPageIntervalTablesToReportTab.IsEnabled = bHasPages & bHasTimingDetails & bHasStepLoad;
            
            btnShowAnalysisNotes.IsEnabled = bHasAnalysisNotes;
            btnAddPerfmonIntervalTablesToReportTab.IsEnabled = bHasStepLoad;

            if(bRunWasAborted)
            {
                btnGenerateReport.IsEnabled = false;
                btnSaveReport.IsEnabled = false;
            }
        }

        private void UpdateTextEditorToolbarVisibility()
        {
            if (Properties.Settings.Default.ShowTextBoxEditorToolbar)
            {
                rtbEditingToolBar.Visibility = System.Windows.Visibility.Visible;
                rtbEditingToolBar.Height = 30;
            }
            else
            {
                rtbEditingToolBar.Visibility = System.Windows.Visibility.Hidden;
                rtbEditingToolBar.Height = 0;
            }
        }
    }
}
